package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.ShoppingBean;

import java.util.ArrayList;

/**
 * ---商品选择数量
 */
public class GoodChooseNumberActivity extends Activity implements View.OnClickListener {
    private ImageView img_goods;
    private TextView tv_name;
    private TextView tv_price;
    private ArrayList<ShoppingBean> list;
    private ShoppingBean shopBean;


    private TextView number;
    private String inventory;
    private double allPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_choose_number);

        initData();

        shopBean = list.get(0);
        initView();
    }

    private void initData() {
        list = (ArrayList<ShoppingBean>) getIntent().getSerializableExtra("list");
        inventory = getIntent().getStringExtra("inventory");
    }

    private void initView() {
        img_goods = (ImageView) findViewById(R.id.img_goods);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_price = (TextView) findViewById(R.id.tv_price);
        number = (TextView) findViewById(R.id.tv_shopping_number);

        findViewById(R.id.close).setOnClickListener(this);
        findViewById(R.id.btn_buy_now).setOnClickListener(this);
        findViewById(R.id.tv_minus).setOnClickListener(this);
        findViewById(R.id.tv_add).setOnClickListener(this);

        ImageLoader.getInstance().displayImage(shopBean.getGoodImg(), img_goods);
        tv_name.setText(shopBean.getGoodName());
        allPrice = Double.parseDouble(shopBean.getGoodPrice()) * num;
        tv_price.setText("¥ " + String.format("%.2f", allPrice));
    }

    private int num = 1;
    private int kucun;

    @Override
    public void onClick(View v) {

        Intent intent;
//        list = new ArrayList<>();
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;

            //减少数目
            case R.id.tv_minus:
                if (num == 1) {
                    Toast.makeText(GoodChooseNumberActivity.this, "再减就没有了", Toast.LENGTH_SHORT).show();
                } else {
                    num--;
                    number.setText(num + "");
                    allPrice = Double.parseDouble(shopBean.getGoodPrice()) * num;
                    tv_price.setText("¥ " + String.format("%.2f", allPrice));
                }
                break;

            //增加数目
            case R.id.tv_add:
                kucun = Integer.parseInt(inventory);
                if (num < kucun) {
                    num++;
                    number.setText(num + "");
                    allPrice = Double.parseDouble(shopBean.getGoodPrice()) * num;
                    tv_price.setText("¥ " + String.format("%.2f", allPrice));
                } else {
                    Toast.makeText(GoodChooseNumberActivity.this, "超出库存数量", Toast.LENGTH_SHORT).show();
                }
                break;

            //立即购买
            case R.id.btn_buy_now:
                list.clear();
                kucun = Integer.parseInt(inventory);
                shopBean.setGoodNumber(num + "");
                list.add(shopBean);
                allPrice = Double.parseDouble(shopBean.getGoodPrice()) * num;
                System.out.println(kucun + "====hhh");
                System.out.println(num + "====hhh");
                if (num <= kucun) {
                    intent = new Intent(GoodChooseNumberActivity.this, ChoosePayActivity.class);
                    intent.putExtra("list", list);
                    intent.putExtra("addType", "1");
                    intent.putExtra("allPrice", String.format("%.2f", allPrice));
                    startActivity(intent);
                } else {
                    Toast.makeText(GoodChooseNumberActivity.this, "超出库存数量", Toast.LENGTH_SHORT).show();
                }

                break;

            default:
                break;
        }


    }
}
